<?php
require_once ("../config/path_config.php");
require_once (ROOTDIR."config/sys_config.php");
require_once (ROOTDIR."common/db_utils.php");
require_once (ROOTDIR."admin/admin-header.php");

$allowAccess = isset($_SESSION['administrator']);
$allowAccess |= isset($_SESSION['problem_editor']);

if ( !$allowAccess ){
	echo "不具备该页面的访问权限";
	exit(1);
}

if ( ! isset($_POST['id']) ) {
    // 获取提交密钥
    echo "
    <h3>请输入本题库的题目ID</h3>
    <form method=POST action=problem_clone.php>";
        require_once("../include/set_post_key.php");
    echo " 
        <input name=id type=text size=100>
        <input type=submit>
    </form>";
}
else {
    require_once("../include/check_post_key.php");
    $userId = $_SESSION['user_id'];
    $oldpid = $_POST['id'];
    $sql = "INSERT INTO problem (`title`, `description`, `input`, `output`, `sample_input`, `sample_output`,";
    $sql .= " spj, hint, source, fromoj, in_date, time_limit, memory_limit, defunct, accepted, submit, ";
    $sql .= " solved, `level`, `creater_id`)";
    $sql .= " (select title, description, input, output, sample_input, sample_output,";
    $sql .= " spj, hint, source, fromoj, NOW(), time_limit, memory_limit, 'Y' as defunct, 0 as accepted, 0 as submit, ";
    $sql .= " 0 as solved, `level`, ? as `creater_id` from problem where problem_id=?); ";
    $sql .= " select @@identity;";

    $newpid =DbUtil::getInstance()->query($sql, $userId, $oldpid);

    echo "题目ID: ".$newpid;
    // 创建测试数据文件夹
    $basedir = SysConfig::$OJ_DATA."/".$newpid;
    mkdir ( $basedir );

    echo "<br><a href='../problem.php?id=".$newpid."'>See the problem</a><br>";
    echo "<br><a href='javascript:phpfm($newpid);'>Add more TestData now !</a>";
}
?>

<script>
function phpfm(pid){
    $.post("phpfm.php", {'frame':3,'pid':pid,'pass':''},
        function(data, status){
            if( status=="success" ) {
                document.location.href="phpfm.php?frame=3&pid="+pid;
            }
        });
}
</script>